import docsObj from "@/views/Home/hooks/docsObj"
import { objEntries } from "howtools"
import { unref } from "vue"

type Method = 'get' | 'post' | 'put' | 'delete'

interface MethodValue {
    deprecated: boolean,
    description: string,
    operationId: string,
    parameters: unknown[],
    produces: unknown[],
    responses: Record<string, Record<string, any>>,
    security: any,
    sunmmary: string
}

type PathType = Record<string, Record<Method, MethodValue>>

export function getPathsV2_0(tag: string, paths: PathType) {
    const result: PathType = {}
    for (const url in paths) {
        const pathVal = paths[url]
        const methodsVal = Object.values(pathVal)
        for (const val of methodsVal) {
            const { tags } = val as unknown as { tags: string[] }
            if (tags.indexOf(tag) > -1) {
                result[url] = pathVal
            }
        }
    }
    return result
}


/**
 * @param  {any} requireParams
 * @description 把参数列表解析分离
 */
export function parserParams(requireParams: any) {
    const request: any = {
        query: [],
        path: [],
        header: [],
        body: [],
    }
    function _paramsGetIn(key: "query" | "path" | "header" | "body" | "formData"): unknown[] {
        return requireParams.filter((it: any) => {
            // 修正后端标记一些错误的swagger类型
            if (!["query", "path", "header", "body", "formData"].includes(it.in)) {
                if (it.in === "Integer") {
                    it.type = "integer"
                } else if (it.in === "String") {
                    it.type = "string"
                } else {
                    it.type = "any"
                }
                it.in = "query"
            }
            it.add = false;
            return it.in == key;
        });
    }
    request.query = _paramsGetIn("query") as [];
    request.path = _paramsGetIn("path") as [];
    request.header = _paramsGetIn("header") as [];

    //解析BODY参数
    const body =
        (_paramsGetIn("body") as { schema: { originalRef: any } }[])[0] ||
        {};
    const oref = body.schema?.originalRef;

    if (oref) {
        for (const [key, val] of objEntries(unref(docsObj).definitions[oref]?.properties || {})) {
            const k = key as string
            request.body.push({
                name: k,
                ...val
            })
        }
    }

    return request
}

// 解析响应值
export function parserResponse(response: any) {
    const resp: any[] = []
    const rref = response.schema?.originalRef || []

    function _getResponse(resp: any[], $rref: string) {
        if ($rref) {
            for (const [key, val] of objEntries(unref(docsObj).definitions[$rref]?.properties || {})) {
                const resObj = {
                    name: key,
                    ...val,
                    sub: []
                }
                
                if(val?.items?.originalRef){
                    _getResponse(resObj.sub, val.items.originalRef)
                }

                if (val.originalRef) {
                    _getResponse(resObj.sub, val.originalRef)
                }
                const k = key as string
                resp.push(resObj)
            }
        }
    }
    _getResponse(resp, rref)
    return resp
}